from django.db import models
from django.conf import settings
from basics import models as basecs_models
from utils.models_save import ModelsSave
from django.contrib.contenttypes.fields import GenericRelation

# Create your models here.

__all__ = [
    'ModulePage',
    'LabelElement',
    'Steps',
    'SeleniumFunc',
    'StepsRealize'
]


# 页面表
class ModulePage(models.Model):
    project = models.ForeignKey(basecs_models.Project, on_delete=True, verbose_name="项目名称")
    module = models.ForeignKey(basecs_models.Module, on_delete=True, verbose_name="所属模块")
    title = models.CharField(max_length=64, verbose_name='标题')
    module_page_key = models.CharField(max_length=64, blank=True, null=True, default=None, verbose_name='页面key')
    create_time = models.DateTimeField(blank=True, null=True, verbose_name="创建日期")
    update_time = models.DateTimeField(blank=True, null=True, verbose_name="更新日期")
    status = models.IntegerField(choices=settings.MODELS_CHOICES['common'], default=2, verbose_name="状态")

    class Meta:
        unique_together = ('module', 'module_page_key',)
        verbose_name = '页面表'
        verbose_name_plural = '页面表'

    def __str__(self):
        return self.title


# 标签定位表
class LabelElement(models.Model):
    project = models.ForeignKey(basecs_models.Project, on_delete=True, verbose_name="项目名称")
    module = models.ForeignKey(basecs_models.Module, on_delete=True, verbose_name="所属模块")
    module_page = models.ForeignKey('ModulePage', on_delete=True, verbose_name='页面')
    title = models.CharField(max_length=64, verbose_name='标题')
    element_type = models.CharField(choices=settings.MODELS_CHOICES['web']['element_type'], default=None,
                                    max_length=32, verbose_name='标签类型')
    element_key = models.CharField(max_length=64, blank=True, null=True, default=None, verbose_name='标签key')
    content = models.CharField(max_length=512, verbose_name='定位数据')
    remark = models.CharField(max_length=256, verbose_name='备注')
    label_location = models.FileField(upload_to='LabelElement/label_location', blank=True, null=True,
                                      verbose_name="标签位置")
    create_time = models.DateTimeField(blank=True, null=True, verbose_name="创建日期")
    update_time = models.DateTimeField(blank=True, null=True, verbose_name="更新日期")
    status = models.IntegerField(choices=settings.MODELS_CHOICES['common'], default=2, verbose_name="状态")

    def save(self, *args, **kwargs):
        old_obj = LabelElement.objects.filter(id=self.id).first()
        rets = ['label_location']
        ModelsSave(old_obj, self, rets).run()  # 删除旧数据
        super(LabelElement, self).save(*args, **kwargs)

    class Meta:
        unique_together = ('module_page', 'element_key',)
        verbose_name = '标签定位表'
        verbose_name_plural = '标签定位表'

    def __str__(self):
        return self.title


# 执行函数
class SeleniumFunc(models.Model):
    label_element = models.ManyToManyField(to='LabelElement', blank=True)
    project = models.ForeignKey(basecs_models.Project, default=None, on_delete=None, verbose_name="项目名称")
    module = models.ForeignKey(basecs_models.Module, default=None, on_delete=None, verbose_name="所属模块")

    sql_detail_body = models.ForeignKey(to=basecs_models.SqlDetail, blank=True, null=True, default=None,
                                        on_delete=models.CASCADE,
                                        related_name="函数入参sql", verbose_name='函数入参sql')
    sql_detail_assert = models.ForeignKey(to=basecs_models.SqlDetail, blank=True, null=True, default=None,
                                          on_delete=models.CASCADE,
                                          related_name="函数断言sql", verbose_name='函数入参sql')

    module_page = models.ForeignKey('ModulePage', default=None, on_delete=None, verbose_name='页面')
    title = models.CharField(max_length=64, verbose_name='标题')
    func_name = models.CharField(max_length=128, verbose_name='函数名称')
    remarks = models.CharField(max_length=256, verbose_name='备注')
    into_example = models.TextField(default=None, blank=True, null=True, verbose_name='入参示例')
    out_example = models.TextField(default=None, blank=True, null=True, verbose_name='出参示例')
    create_time = models.DateTimeField(blank=True, null=True, verbose_name="创建日期")
    update_time = models.DateTimeField(blank=True, null=True, verbose_name="更新日期")
    status = models.IntegerField(choices=settings.MODELS_CHOICES['common'], default=2, verbose_name="状态")

    class Meta:
        unique_together = ('module_page', 'func_name',)
        verbose_name = '执行函数'
        verbose_name_plural = '执行函数'

    def __str__(self):
        return self.title


# 步骤表
class Steps(models.Model):
    project = models.ForeignKey(basecs_models.Project, on_delete=True, verbose_name="项目名称")
    module = models.ForeignKey(basecs_models.Module, on_delete=True, verbose_name="所属模块")
    obj_key = models.CharField(max_length=128, unique=True, blank=True, null=True, default=None, verbose_name='步骤key')
    # module_page = models.ForeignKey(max_length=40, unique=True, blank=True, null=True, default=None)
    title = models.CharField(max_length=64, verbose_name='标题')
    steps_data = models.TextField(blank=True, null=True, default=None, verbose_name="步骤数据")
    create_time = models.DateTimeField(blank=True, null=True, verbose_name="创建日期")
    update_time = models.DateTimeField(blank=True, null=True, verbose_name="更新日期")
    results = models.IntegerField(choices=settings.MODELS_CHOICES['results'], default=3, verbose_name='执行状态')
    status = models.IntegerField(choices=settings.MODELS_CHOICES['common'], default=2, verbose_name="状态")

    class Meta:
        verbose_name = 'WEB步骤表'
        verbose_name_plural = 'WEB步骤表'

    def __str__(self):
        return self.title


# 步骤实现表
class StepsRealize(models.Model):
    project = models.ForeignKey(basecs_models.Project, default=None, on_delete=None, verbose_name="项目名称")
    module = models.ForeignKey(basecs_models.Module, default=None, on_delete=None, verbose_name="所属模块")
    module_page = models.ForeignKey('ModulePage', default=None, on_delete=None, verbose_name='页面')
    steps = models.ForeignKey('Steps', on_delete=True, verbose_name='步骤')
    selenium_func = models.ForeignKey('SeleniumFunc', on_delete=True, verbose_name='执行函数')
    obj_key = models.CharField(max_length=128, unique=True, default=None, blank=True, null=True, verbose_name='步骤实现key')
    title = models.CharField(max_length=64, verbose_name='标题')

    into_sql_push = models.TextField(blank=True, null=True, default=None, verbose_name="web查询sql入参")  # 入参模板，
    into_sql_pull = models.TextField(blank=True, null=True, default=None, verbose_name="web查询sql出参")  # 请求体模板
    assert_sql_push = models.TextField(blank=True, null=True, default=None, verbose_name="web断言sql入参")  # 入参模板，
    assert_sql_pull = models.TextField(blank=True, null=True, default=None, verbose_name="web断言sql出参")

    into_template = models.TextField(blank=True, null=True, verbose_name='入参模板')
    out_template = models.TextField(blank=True, null=True, verbose_name='出参模板')
    assert_template = models.TextField(blank=True, null=True, verbose_name='断言模板')
    into_data = models.TextField(blank=True, null=True, verbose_name='入参数据')
    out_data = models.TextField(blank=True, null=True, verbose_name='出参数据')
    page_out_data = models.TextField(blank=True, null=True, verbose_name='页面数据')
    assert_data = models.TextField(blank=True, null=True, verbose_name='断言数据')
    result_data = models.TextField(blank=True, null=True, verbose_name='对比结果')

    failure_img = models.FileField(upload_to='StepsRealize/failure_img', blank=True, null=True, verbose_name="失败截图")
    create_time = models.DateTimeField(blank=True, null=True, verbose_name="创建日期")
    update_time = models.DateTimeField(blank=True, null=True, verbose_name="更新日期")
    sort = models.IntegerField(choices=settings.MODELS_CHOICES['sort'], verbose_name="步骤顺序")
    results = models.IntegerField(choices=settings.MODELS_CHOICES['results'], default=3, verbose_name='执行状态')
    show_status = models.BooleanField(default=True, verbose_name='显示正确结果')  # 是否显示正确结果
    # test_status = models.BooleanField(default=True, verbose_name='可否单独测试')  # 可否单独测试
    status = models.IntegerField(choices=settings.MODELS_CHOICES['common'], verbose_name="状态")
    cases_detail = GenericRelation(to=basecs_models.CasesDetail)  # 与用例明细反向查询用，不生成字段

    def save(self, *args, **kwargs):
        old_obj = StepsRealize.objects.filter(id=self.id).first()
        rets = ['failure_img']
        ModelsSave(old_obj, self, rets).run()  # 删除旧数据
        super(StepsRealize, self).save(*args, **kwargs)

    class Meta:
        unique_together = ('steps', 'sort',)
        verbose_name = '步骤实现表'
        verbose_name_plural = '步骤实现表'

    def __str__(self):
        return self.title
